package com.aplink.generic.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.aplink.generic.pojo.MainMenu;
import com.aplink.generic.pojo.MainMenuItem;
import com.aplink.sg.R;

public class NavigationDrawerAdapter extends BaseAdapter {
    private static class ViewHolder {
        public ImageView image;
        public TextView text;
    }

    private final Activity mContext;

    private final MainMenu mMainMenu;

    public NavigationDrawerAdapter(final Activity context,
            final MainMenu mainMenu) {
        this.mContext = context;
        this.mMainMenu = mainMenu;
    }

    @Override
    public int getCount() {
        return this.mMainMenu.size();
    }

    @Override
    public MainMenuItem getItem(final int position) {
        return this.mMainMenu.valueAt(position);
    }

    @Override
    public long getItemId(final int position) {
        return this.mMainMenu.keyAt(position);
    }

    @Override
    public View getView(final int position, final View convertView,
            final ViewGroup parent) {
        View rowView = convertView;
        if (rowView == null) {
            final LayoutInflater inflater = LayoutInflater.from(this.mContext);
            rowView = inflater.inflate(R.layout.aplink__generic_navigate_item,
                    parent, false);
            final ViewHolder viewHolder = new ViewHolder();
            viewHolder.text = (TextView) rowView.findViewById(R.id.menu_title);
            viewHolder.image = (ImageView) rowView.findViewById(R.id.menu_logo);
            rowView.setTag(viewHolder);
        }
        final ViewHolder holder = (ViewHolder) rowView.getTag();
        final MainMenuItem mainMenuItem = getItem(position);
        holder.text.setText(mainMenuItem.getTitle());
        holder.image.setImageResource(mainMenuItem.getIconID());
        return rowView;
    }

}
